import 'package:flutter/material.dart';
import 'package:local_coffee/components/c_appbar/custom_appbar.dart';
import 'package:local_coffee/components/c_button/index.dart';
import 'package:local_coffee/components/hex.dart';
import 'package:local_coffee/components/icon.dart';
import 'package:local_coffee/components/rgba.dart';
import 'package:local_coffee/components/take_out_btn/take_out_btn.dart';
import 'package:local_coffee/jsonserialize/order/data.dart';
import 'package:local_coffee/pages/order/widgets/goods_msg_row.dart';
import 'package:local_coffee/provider/order_model.dart';
import 'package:local_coffee/provider/shopping_cart_model.dart';
import 'package:local_coffee/routes/index.dart';
import 'package:local_coffee/utils/global.dart';
import 'package:provider/provider.dart';

class OrderConfirm extends StatefulWidget {
  
  OrderConfirm({
    Key key
  }) : super(key: key);

  _OrderConfirmState createState() => _OrderConfirmState();
}

class _OrderConfirmState extends State<OrderConfirm> {
  
  double bottom = G.screenPadding().bottom;

  @override
  Widget build(BuildContext context) {
    
    ShoppingCartModel _shoppingCartModel = Provider.of<ShoppingCartModel>(context);

    OrderModel _orderModel = Provider.of<OrderModel>(context);

    Map<String, OrderData> _orderData = _orderModel.data;

    return Scaffold(
      appBar: customAppBar(
        title: '确认订单',
        context: context
      ),
      body: Stack(
        children: <Widget>[
          Container(),
          SingleChildScrollView(
            child: Container(
              child: Column(
                children: <Widget>[
                  head(),
                  Container(
                    margin: EdgeInsets.only(top: 10),
                    color: HEX.hexfff,
                    child: Column(
                      children: <Widget>[
                        contentAddress(),
                        // 订单信息
                        Container(
                          padding: EdgeInsets.only(top: 12, left: 15, right: 15),
                          decoration: BoxDecoration(
                            border: G.borderBottom()
                          ),
                          child: Column(
                            children: <Widget>[
                              Container(
                                margin: EdgeInsets.only(bottom: 12),
                                child: Row(
                                  children: <Widget>[
                                    Text(
                                      '订单信息',
                                      style: TextStyle(
                                        color: RGBA.rgba383838,
                                        fontSize: 13,
                                        fontWeight: FontWeight.bold
                                      ),
                                    )
                                  ],
                                ),
                              ),
                              Column(
                                children: _orderData.keys.map((key) {
                                  return GoodsMsgRow(
                                    data: _shoppingCartModel.data[key],
                                  );
                                }).toList(),
                              )
                            ],
                          ),
                        ),
                        // 结算
                        Container(
                          padding: EdgeInsets.symmetric(
                            vertical: 12,
                            horizontal: 13
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  Text(
                                    '合计',
                                    style: TextStyle(
                                      color: RGBA.rgba505050,
                                      fontSize: 13
                                    ),
                                  ),
                                  Container(
                                    margin: EdgeInsets.only(left: 10),
                                    child: Text(
                                      '￥ ${_shoppingCartModel.totalPrice}',
                                      style: TextStyle(
                                        color: RGBA.rgba383838,
                                        fontSize: 16,
                                        fontWeight: FontWeight.bold
                                      ),
                                    ),
                                  )
                                ],
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                  // 支付信息
                  Container(
                    margin: EdgeInsets.only(top: 10),
                    padding: EdgeInsets.symmetric(horizontal: 15),
                    color: HEX.hexfff,
                    child: Column(
                      children: <Widget>[
                        // 使用优惠
                        InkWell(
                          child: Container(
                            padding: EdgeInsets.symmetric(vertical: 14),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Row(
                                  children: <Widget>[
                                    Text(
                                      '使用优惠券',
                                      style: TextStyle(
                                        color: RGBA.rgba88afd5,
                                        fontSize: 14
                                      ),
                                    )
                                  ],
                                ),
                                Row(
                                  children: <Widget>[
                                    Text(
                                      '1个使用',
                                      style: TextStyle(
                                        color: RGBA.rgbaff8d1a,
                                        fontSize: 14
                                      ),
                                    ),
                                    Container(
                                      margin: EdgeInsets.only(left: 5),
                                      child: icon0xe60f(
                                        color: RGBA.rgbae4e4e4,
                                        size: 16
                                      ),
                                    )
                                  ],
                                )
                              ],
                            ),
                          ),
                          onTap: () {
                            Navigator.pushNamed(context, N.coupon);
                          },
                        )
                      ],
                    ),
                  ),
                  // 备注
                  Container(
                    height: 50,
                    margin: EdgeInsets.only(top: 10),
                    padding: EdgeInsets.symmetric(horizontal: 15),
                    color: HEX.hexfff,
                    child: InkWell(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text(
                            '备注：特殊要求',
                            style: TextStyle(
                              color: RGBA.rgba383838,
                              fontSize: 14
                            ),
                          ),
                          icon0xe60f(
                            color: RGBA.rgbae4e4e4,
                            size: 16
                          )
                        ],
                      ),
                      onTap: () => Navigator.pushNamed(context, N.orderConfirm),
                    ),
                  ),
                  // 协议
                  Container(
                    margin: EdgeInsets.only(top: 15),
                    padding: EdgeInsets.symmetric(horizontal: 15),
                    child: Column(
                      children: <Widget>[
                        Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                            Container(
                              width: 20,
                              height: 20,
                              child: Transform.scale(
                                scale: .8,
                                child: Checkbox(
                                  activeColor: RGBA.rgba88afd5,
                                  value: true,
                                  onChanged: (flag) {},
                                ),
                              ),
                            ),
                            Container(
                              child: Row(
                                children: <Widget>[
                                  Text(
                                    '我已阅读并同意',
                                    style: TextStyle(
                                      color: RGBA.rgba808080,
                                      fontSize: 10
                                    ),
                                  ),
                                  Text(
                                    '支持协议',
                                    style: TextStyle(
                                      color: RGBA.rgba557a9d,
                                      fontSize: 10
                                    ),
                                  )
                                ],
                              ),
                            )
                          ],
                        ),
                        Container(
                          margin: EdgeInsets.only(top: 5),
                          child: Row(
                            children: <Widget>[
                              Text(
                                '温馨提示：仅支持开具电子发票，订单完成后可前往发票管理中开具',
                                style: TextStyle(
                                  color: RGBA.rgbaa6a6a6,
                                  fontSize: 10
                                ),
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  )
                ],
              ),
            ),
          ),
          // 底部
          Positioned(
            bottom: 0,
            left: 0,
            child: Container(
              padding: EdgeInsets.only(left: 15, bottom: bottom),
              width: G.screenWidth(),
              color: HEX.hexfff,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Text(
                        '还需支付',
                        style: TextStyle(
                          color: RGBA.rgba383838,
                          fontSize: 14,
                          fontWeight: FontWeight.bold
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(left: 10),
                        child: Text(
                          '￥ ${_shoppingCartModel.totalPrice}',
                          style: TextStyle(
                            color: RGBA.rgba383838,
                            fontSize: 24,
                            fontWeight: FontWeight.bold
                          ),
                        ),
                      )
                    ],
                  ),
                  CButton.normal(
                    child: Text('去支付'),
                    bgColor: RGBA.rgba94c4ec,
                    width: 120,
                    height: 60,
                    color: RGBA.rgbaffffff1,
                    borderRadius: BorderRadius.zero,
                    onPressed: () => G.pushNamed(N.orderDetail, arguments: {'status': 1})
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  /// 地址
  Container contentAddress() {
    return Container(
      decoration: BoxDecoration(
        border: G.borderBottom()
      ),
      padding: EdgeInsets.symmetric(vertical: 12, horizontal: 15),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Text(
                '自提门店',
                style: TextStyle(
                  color: RGBA.rgba383838,
                  fontSize: 13,
                  fontWeight: FontWeight.bold
                ),
              )
            ],
          ),
          InkWell(
            onTap: () => Navigator.pushNamed(context, N.selfStore),
            child: Column(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(top: 8),
                  child: Row(
                    children: <Widget>[
                      Text(
                        '青年店(No.1795)',
                        style: TextStyle(
                          color: RGBA.rgba383838,
                          fontSize: 15
                        ),
                      )
                    ],
                  ),
                ),
                Row(
                  children: <Widget>[
                    Text(
                      'xx区xx路xx楼xx层xxx室',
                      style: TextStyle(
                        color: RGBA.rgba383838,
                        fontSize: 12
                      ),
                    )
                  ],
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  /// 头
  Container head() {
    return Container(
      height: 80,
      padding: EdgeInsets.symmetric(horizontal: 15),
      alignment: Alignment.center,
      color: HEX.hexfff,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          TakeOutBtn(),
          Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  '立即取餐',
                  style: TextStyle(
                    color: RGBA.rgba383838,
                    fontSize: 13,
                    fontWeight: FontWeight.bold
                  ),
                ),
                Row(
                  children: <Widget>[
                    Text(
                      '约',
                      style: TextStyle(
                        color: RGBA.rgba808080,
                        fontSize: 13
                      ),
                    ),
                    Text(
                      '12:12',
                      style: TextStyle(
                        color: RGBA.rgba88afd5,
                        fontSize: 20,
                        fontWeight: FontWeight.bold
                      ),
                    ),
                    Text(
                      '可取',
                      style: TextStyle(
                        color: RGBA.rgba808080,
                        fontSize: 13
                      ),
                    )
                  ],
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}
